<template>
  <view class="page">
    <header-vue :back="true" :title="title"></header-vue>
    <view class="page-content">
      <!-- 顶部暖芯值信息 -->
      <view class="warm-value-section">
        <image
           :src="IMAGE_URL+'/static/user/nxz-bg.png'"
          style="width: 100%; height: 320rpx"
        />
        <view class="value-card">
          <view class="value-header">
            <image
               :src="IMAGE_URL+'/static/user/sygz.png'"
              style="width: 116rpx; height: 50rpx"
            />
            <view class="rule-btn" @click="showRules">使用规则</view>
          </view>
          <view class="value-content">
            <view class="main-value">
              <view class="value-label">暖芯值(元)</view>
              <view class="value-amount">
                <text class="value-m">￥</text>
                <text>634.98</text>
              </view>
            </view>
            <view class="pending-value">
              <view class="pending-label">待入账(元)</view>
              <view class="pending-amount">
                <text class="value-m">￥</text>
                <text>34.98</text>
              </view>
            </view>
          </view>
        </view>
      </view>

      <!-- 明细选项卡 -->
      <view class="tabs-section">
        <view class="tabs">
          <view
            class="tab-item"
            :class="{ active: activeTab === 'income' }"
            @click="switchTab('income')"
          >
            <text>收入明细</text>
          </view>
          <view
            class="tab-item"
            :class="{ active: activeTab === 'expense' }"
            @click="switchTab('expense')"
          >
            <text>支出明细</text>
          </view>
        </view>
      </view>

      <!-- 时间选择器 -->
      <view class="time-filter-section">
        <view class="time-picker-wrapper">
          <view class="time-display" @click="showTimePicker = true">
            <text class="time-text">{{ formattedMonth }}</text>
            <u-icon name="arrow-down" size="14" color="#666"></u-icon>
          </view>
          <u-datetime-picker
            :value="selectedTime"
            mode="date"
            :show="showTimePicker"
            @confirm="onTimeConfirm"
            @cancel="showTimePicker = false"
          ></u-datetime-picker>
        </view>
      </view>

      <!-- 明细列表 -->
      <view class="records-section">
        <!-- 收入明细 -->
        <view v-if="activeTab === 'income'" class="income-records">
          <view
            v-for="(record, index) in filteredIncomeRecords"
            :key="index"
            class="record-item"
          >
            <view class="record-left-box">
              <image
                 :src="IMAGE_URL+'/static/user/srmx.png'"
                style="width: 68rpx; height: 76rpx"
              />
              <view class="record-left">
                <text class="record-type">消费赠送</text>
                <text class="record-time">{{ record.time }}</text>
              </view>
            </view>
            <view class="record-right">
              <text class="record-amount">+{{ record.amount }}</text>
            </view>
          </view>
        </view>

        <!-- 支出明细 -->
        <view v-if="activeTab === 'expense'" class="expense-records">
          <view
            v-for="(record, index) in filteredExpenseRecords"
            :key="index"
            class="record-item"
          >
            <view class="record-left-box">
              <image
                 :src="IMAGE_URL+'/static/user/zcmx.png'"
                style="width: 68rpx; height: 76rpx"
              />
              <view class="record-left">
                <text class="record-type">{{ record.type }}</text>
                <text class="record-time">{{ record.time }}</text>
              </view>
            </view>

            <view class="record-right">
              <text class="record-amount expense-amount"
                >-{{ record.amount }}</text
              >
            </view>
          </view>
        </view>
      </view>

      <useRulePopup
        :show="showUseRulePopup"
        @close="showUseRulePopup = false"
      />
    </view>
  </view>
</template>

<script>
import headerVue from "@/components/header.vue";
//引入use-rule-popup组件
import useRulePopup from "./components/use-rule-popup.vue";
import appConfig from '@/config/app.js'
//  IMAGE_URL: appConfig.IMAGE_URL,
export default {
  data() {
    return {
      IMAGE_URL: appConfig.IMAGE_URL,
      showUseRulePopup: false, // 控制使用规则弹窗的显示与隐藏
      title: "暖芯值",
      activeTab: "income",
      showTimePicker: false,
      selectedTime: Date.now(), // 默认当前时间
      incomeRecords: [
        {
          time: "2025-04-06 15:45",
          amount: "40.90",
          timestamp: new Date(2025, 3, 6, 15, 45).getTime(),
        },
        {
          time: "2025-04-06 15:45",
          amount: "40.90",
          timestamp: new Date(2025, 3, 6, 15, 45).getTime(),
        },
        {
          time: "2025-04-06 15:45",
          amount: "40.90",
          timestamp: new Date(2025, 3, 6, 15, 45).getTime(),
        },
        {
          time: "2025-04-06 15:45",
          amount: "40.90",
          timestamp: new Date(2025, 3, 6, 15, 45).getTime(),
        },
        {
          time: "2025-04-06 15:45",
          amount: "40.90",
          timestamp: new Date(2025, 3, 6, 15, 45).getTime(),
        },
      ],
      expenseRecords: [
        {
          type: "服务消费",
          time: "2025-04-06 15:45",
          amount: "40.90",
          timestamp: new Date(2025, 3, 6, 15, 45).getTime(),
        },
        {
          type: "商品购买",
          time: "2025-04-05 14:30",
          amount: "20.00",
          timestamp: new Date(2025, 3, 5, 14, 30).getTime(),
        },
        {
          type: "会员服务",
          time: "2025-04-04 10:15",
          amount: "30.00",
          timestamp: new Date(2025, 3, 4, 10, 15).getTime(),
        },
      ],
    };
  },
  components: {
    useRulePopup,
    headerVue,
  },
  computed: {
    // 格式化显示的时间
    formattedTime() {
      const date = new Date(this.selectedTime);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      return `${year}-${month}-${day}`;
    },

    // 格式化月份显示
    formattedMonth() {
      const date = new Date(this.selectedTime);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      return `${year}/${month}`;
    },

    // 根据选择的时间过滤收入记录
    filteredIncomeRecords() {
      return this.incomeRecords;
    },

    // 根据选择的时间过滤支出记录
    filteredExpenseRecords() {
      return this.expenseRecords;
    },
  },
  methods: {
    // 切换选项卡
    switchTab(tab) {
      this.activeTab = tab;
    },

    // 显示使用规则
    showRules() {
      this.showUseRulePopup = true;
    },

    // 时间选择确认
    onTimeConfirm(e) {
      this.selectedTime = e.value;
      this.showTimePicker = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.page {
  height: 100vh;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  width: 100%;
  overflow: hidden;
  position: relative;
  background-color: #fff;

  .page-content {
    flex: 1;
    box-sizing: border-box;
    width: 100%;
    overflow-y: auto;
    padding: 28rpx 32rpx;
  }
}

.warm-value-section {
  width: 100%;
  height: 320rpx;
  position: relative;
  overflow: hidden;
  box-sizing: border-box;
}

.value-card {
  position: absolute;
  top: 0rpx;
  left: 0rpx;
  width: 100%;
  height: 320rpx;
}

.value-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
  position: absolute;
  right: 0rpx;
  top: 40rpx;
}

.value-title {
  font-size: 32rpx;
  color: #333;
  font-weight: 600;
}

.rule-btn {
  position: absolute;
  left: 0rpx;
  top: 0rpx;
  width: 116rpx;
  font-size: 24rpx;
  color: #0785cf;
  height: 40rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.value-content {
  display: flex;
  width: 100%;
  height: 100%;

  box-sizing: border-box;
  padding: 48rpx 32rpx;
}

.main-value,
.pending-value {
  display: flex;
  flex-direction: column;
}
.main-value {
  margin-right: 80rpx;
}

.value-label,
.pending-label {
  font-size: 24rpx;
  color: #ffffff;
  margin-bottom: 10rpx;
}
.value-m {
  font-size: 24rpx;
  color: #ffffff;
}

.value-amount {
  font-size: 32rpx;
  color: #ffffff;
}

.pending-amount {
  font-size: 32rpx;
  color: #ffffff;
  font-weight: 600;
}

.tabs-section {
  background: #fff;
  padding: 0 30rpx;
}

.tabs {
  display: flex;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 30rpx 0 15rpx 0;
  font-size: 28rpx;
  color: #666;
  position: relative;

  &.active {
    font-weight: 600;

    color: #333;
    &::after {
      content: "";
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translateX(-50%);
      width: 60rpx;
      height: 4rpx;
      background: #0785cf;
      border-radius: 2rpx;
    }
  }
}

/* 时间选择器样式 */
.time-filter-section {
  background: #fff;
  padding: 20rpx 0rpx;
}

.time-picker-wrapper {
  display: flex;
  flex-direction: column;
}

.time-label {
  font-size: 26rpx;
  color: #666;
  margin-right: 15rpx;
}

.time-display {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 166rpx;
  height: 52rpx;
  background: #ffffff;
  box-shadow: 0rpx 0rpx 16rpx 0rpx rgba(0, 0, 0, 0.08);
  border-radius: 60rpx 60rpx 60rpx 60rpx;
}

.time-text {
  font-size: 26rpx;
  color: #333;
  font-weight: 500;
}

.records-section {
  background: #fff;
  flex: 1;
}

.month-text {
  font-size: 24rpx;
  color: #333333;
  margin-right: 4rpx;
  font-weight: 500;
}

.record-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx 28rpx;
  box-sizing: border-box;
  width: 100%;
  background: #ffffff;
  box-shadow: 0rpx 4rpx 20rpx 0rpx rgba(0, 0, 0, 0.08);
  border-radius: 20rpx 20rpx 20rpx 20rpx;
  margin-bottom: 28rpx;
}

.record-left-box {
  display: flex;
  align-items: center;
}

.record-left {
  display: flex;
  flex-direction: column;
  margin-left: 20rpx;
}

.record-type {
  font-size: 28rpx;
  color: #333;
  font-weight: 500;
  margin-bottom: 8rpx;
}

.record-time {
  font-size: 24rpx;
  color: #999;
}

.record-right {
  display: flex;
  align-items: center;
}

.record-amount {
  font-size: 32rpx;
  font-weight: 600;
  color: #52c41a;

  &.expense-amount {
    color: #e74c3c;
  }
}
</style>
